From 9226671f49b507ce6f71e6e2c3621014f05f74e9 Mon Sep 17 00:00:00 2001
From: Armand Philippot <git@armandphilippot.com>
Date: Wed, 23 Mar 2022 22:05:30 +0100
Subject: refactor: load prism plugins without babel

---
 src/pages/article/[slug].tsx | 58 ++++++++++++++++++++++++++++++++------------
 1 file changed, 43 insertions(+), 15 deletions(-)

(limited to 'src/pages/article/[slug].tsx')

diff --git a/src/pages/article/[slug].tsx b/src/pages/article/[slug].tsx
index 41b84b6..27a6f7b 100644
--- a/src/pages/article/[slug].tsx
+++ b/src/pages/article/[slug].tsx
@@ -14,21 +14,20 @@ import {
 import styles from '@styles/pages/Page.module.scss';
 import { NextPageWithLayout } from '@ts/types/app';
 import { ArticleMeta, ArticleProps } from '@ts/types/articles';
+import { PrismDefaultPlugins, PrismPlugins } from '@ts/types/prism';
 import { settings } from '@utils/config';
 import { getFormattedPaths } from '@utils/helpers/format';
 import { loadTranslation } from '@utils/helpers/i18n';
 import { addPrismClasses } from '@utils/helpers/prism';
-import { usePrismTheme } from '@utils/providers/prism';
 import { GetStaticPaths, GetStaticProps, GetStaticPropsContext } from 'next';
 import Head from 'next/head';
 import { useRouter } from 'next/router';
-import { highlightAll } from 'prismjs';
+import Script from 'next/script';
+import Prism from 'prismjs';
 import { ParsedUrlQuery } from 'querystring';
-import { useEffect } from 'react';
+import { useCallback, useEffect, useMemo } from 'react';
 import { useIntl } from 'react-intl';
 import { Blog, BlogPosting, Graph, WebPage } from 'schema-dts';
-import '@utils/plugins/prism-color-scheme';
-import Script from 'next/script';
 
 const SingleArticle: NextPageWithLayout<ArticleProps> = ({
   comments,
@@ -37,19 +36,48 @@ const SingleArticle: NextPageWithLayout<ArticleProps> = ({
   const intl = useIntl();
   const router = useRouter();
 
-  useEffect(() => {
-    addPrismClasses();
-    highlightAll();
-  });
+  const loadPrismPlugins = useCallback(
+    async (prismPlugins: (PrismDefaultPlugins | PrismPlugins)[]) => {
+      for (const plugin of prismPlugins) {
+        try {
+          if (plugin === 'color-scheme') {
+            await import(`@utils/plugins/prism-${plugin}`);
+          } else {
+            await import(`prismjs/plugins/${plugin}/prism-${plugin}.min.js`);
 
-  const { setCodeBlocks } = usePrismTheme();
+            if (plugin === 'autoloader')
+              Prism.plugins.autoloader.languages_path = '/prism/';
+          }
+        } catch (error) {
+          console.error('Article: an error occurred with Prism.');
+          console.error(error);
+        }
+      }
+    },
+    []
+  );
+
+  const plugins: (PrismDefaultPlugins | PrismPlugins)[] = useMemo(
+    () => [
+      'autoloader',
+      'toolbar',
+      'show-language',
+      'copy-to-clipboard',
+      'color-scheme',
+      'command-line',
+      'line-numbers',
+      'match-braces',
+      'normalize-whitespace',
+    ],
+    []
+  );
 
   useEffect(() => {
-    const allPre: NodeListOf<HTMLPreElement> = document.querySelectorAll(
-      'pre[data-prismjs-color-scheme-current]'
-    );
-    setCodeBlocks(allPre);
-  }, [setCodeBlocks, router.asPath]);
+    loadPrismPlugins(plugins).then(() => {
+      addPrismClasses();
+      Prism.highlightAll();
+    });
+  }, [plugins, loadPrismPlugins]);
 
   if (router.isFallback) return <Spinner />;
 
-- 
cgit v1.2.3

